﻿using System;
using System.Collections.Generic;

using Matchingo.Common.Constants;
using Matchingo.Common.Types.Interfaces;

namespace Matchingo.Models.BackgroundProvider
{
    public class PathBackgroundProvider : IBackgroundSetProvider
    {
        #region ========== Private Properties ==========

        private IGameSettingsProvider SettingsProvider { get; set; }
		private List<IBackgroundSetResources> BackgroundSetResources { get; set; }

        #endregion

        #region ========== Public Properties ==========

		public List<string> BackgroundSetsAvailable
		{
			get { return GetBackgroundSetNameList(); }
		}

		public List<Uri> BackgroundImages
        {
            get { return GetCurrentBackgroundSet(); }
        }

        #endregion

        #region ========== Constructor ==========

		public PathBackgroundProvider(IGameSettingsProvider settingsProvider, List<IBackgroundSetResources> backgroundSetResources)
        {
			SettingsProvider = settingsProvider;
            BackgroundSetResources = backgroundSetResources;
        }

        #endregion

        #region ========== Private Methods ==========

		private List<string> GetBackgroundSetNameList()
		{
			List<string> backgroundSetNames = new List<string>();

			foreach (var backgroundResources in BackgroundSetResources)
			{
				backgroundSetNames.AddRange(backgroundResources.BackgroundSetNames);
			}

			return backgroundSetNames;
		}

		private List<Uri> GetCurrentBackgroundSet()
		{
			// if we are to get random background then return all the sets combined ..
			if (SettingsProvider.GameSettings.BackgroundSetName == CommonConstants.GAME_SETTINGS_IMAGESETS_RANDOM)
			{
				List<Uri> randomBackgroundSetImages = new List<Uri>();

				// go through all the card providers (plugins) and find all the sets they provide, and add
				// them all to our list of card images
				foreach (var backgroundResources in BackgroundSetResources)
				{
					Dictionary<string, List<Uri>> dict = backgroundResources.BackgroundSetsProvided;
					foreach (var key in dict.Keys)
					{
						randomBackgroundSetImages.AddRange(dict[key]);
					}
				}

				return randomBackgroundSetImages;
			}

		    List<Uri> backgroundSetImages = GetBackgroundResourcesFromName(BackgroundSetResources, 
                                                                           SettingsProvider.GameSettings.BackgroundSetName);
			// we werent able to find the cardset set in the users options.. this might happen if that cardset is no longer available
			// in which case this should already have been handled elsewhere, so we will throw an exception
			if (backgroundSetImages == null)
			{
				// forcefully set their settings back to random ..
				SettingsProvider.GameSettings.BackgroundSetName = CommonConstants.GAME_SETTINGS_IMAGESETS_RANDOM;
				SettingsProvider.SaveSettings();

				// .. and call ourself to get a random set
				return GetCurrentBackgroundSet();
			}

			return backgroundSetImages;
		}

		private static List<Uri> GetBackgroundResourcesFromName(IEnumerable<IBackgroundSetResources> backgroundResourcesList, string name)
		{
			foreach (IBackgroundSetResources backgroundResources in backgroundResourcesList)
			{
				if (backgroundResources.BackgroundSetNames.Contains(name))
				{
					return backgroundResources.BackgroundSetsProvided[name];
				}
			}

			return null;
		}

        #endregion
    }
}
